﻿using System;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain.Authentication;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class ConfigureFirstSite : AdminBasePage, IConfigureFirstSiteView
    {
        private ConfigureFirstSitePresenter _presenter;

        #region IConfigureFirstSiteView Members

        public string Domain
        {
            get { return txtDomain.Text; }
            set { txtDomain.Text = value; }
        }


        public string DefaultLocaleName
        {
            get { return txtDefaultLocaleName.Text; }
            set { txtDefaultLocaleName.Text = value; }
        }


        public string DefaultLocaleRegion
        {
            get { return txtDefaultLocaleRegion.Text; }
            set { txtDefaultLocaleRegion.Text = value; }
        }


        public string DefaultLocaleLanguage
        {
            get { return txtDefaultLocaleLanguage.Text; }
            set { txtDefaultLocaleLanguage.Text = value; }
        }


        public string DefaultLocaleCode
        {
            get { return txtDefaultLocaleCode.Text; }
            set { txtDefaultLocaleCode.Text = value; }
        }


        public string RootMediaFolder
        {
            get { return txtRootMediaFolder.Text; }
            set { txtRootMediaFolder.Text = value; }
        }


        public string ContextTranslationFile
        {
            get { return "Admin/Translations/ConfigureFirstSite.xml"; }
        }


        public string UserPhysicalTempDirectory
        {
            get { return "Admin\\Resources\\Temp\\" + AdministratorID + "\\"; }
        }


        public string UserTempDirectory
        {
            get { return Url.FullPath + "Admin/Resources/Temp/" + AdministratorID + "/"; }
        }


        public virtual int AdministratorID
        {
            get
            {
                AuthenticatedUser user = Conversion.ToAuthenticatedUser(User.Identity.Name);
                return user.AdministratorID;
            }
        }


        public void ConfigureFirstSiteNotAllowed()
        {
            Response.Redirect("Dashboard.aspx");
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new ConfigureFirstSitePresenter(this);
            pnlError.Visible = false;
            if (Page.IsPostBack)
                return;
            _presenter.Initialize();
            string url = Request.Url.Host;
            if (Request.Url.Port != 80)
                url += ":" + Request.Url.Port;
            url += Request.ApplicationPath;
            if (url.EndsWith("/"))
                url = url.TrimEnd("/".ToCharArray());
            txtDomain.Text = url;
            txtDomain.Focus();
        }


        protected void lbSave_Click(object sender, EventArgs e)
        {
            _presenter.Save();
            if (!_presenter.IsValid)
            {
                txtDomain.Focus();
                pnlError.Visible = true;
                return;
            }
            Response.Redirect("Dashboard.aspx");
        }
    }
}